#include "filterwidget.h"
#include <QLineEdit>
#include <QComboBox>
#include <QDateEdit>
#include <QLabel>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QFormLayout>
#include <QGroupBox>

FilterWidget::FilterWidget(const QVector<FilterConfig> &filters, QWidget *parent)
    : QWidget(parent), m_filters(filters)
{
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    QGroupBox *filterGroup = new QGroupBox("查询条件", this);
    QFormLayout *formLayout = new QFormLayout(filterGroup);

    // 创建筛选控件
    for (const FilterConfig &filter : filters) {
        QWidget *filterControl = createFilterControl(filter);
        if (filterControl) {
            formLayout->addRow(filter.name + ":", filterControl);
        }
    }

    // 创建按钮区域
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    m_resetButton = new QPushButton("重置", this);
    m_searchButton = new QPushButton("查询", this);
    m_searchButton->setDefault(true);

    buttonLayout->addStretch();
    buttonLayout->addWidget(m_resetButton);
    buttonLayout->addWidget(m_searchButton);

    mainLayout->addWidget(filterGroup);
    mainLayout->addLayout(buttonLayout);

    // 连接信号
    connect(m_resetButton, &QPushButton::clicked, this, &FilterWidget::onResetFilters);
    connect(m_searchButton, &QPushButton::clicked, this, &FilterWidget::filterChanged);

    setLayout(mainLayout);
}

QWidget* FilterWidget::createFilterControl(const FilterConfig &filter)
{
    QWidget *control = nullptr;

    if (filter.type == "text") {
        control = createTextFilter(filter);
    } else if (filter.type == "daterange") {
        control = createDateRangeFilter(filter);
    } else if (filter.type == "combobox") {
        control = createComboBoxFilter(filter);
    }

    if (control) {
        m_filterWidgets[filter.field] = control;
    }

    return control;
}

QWidget* FilterWidget::createTextFilter(const FilterConfig &filter)
{
    QLineEdit *lineEdit = new QLineEdit(this);
    lineEdit->setPlaceholderText("请输入" + filter.name);

    connect(lineEdit, &QLineEdit::returnPressed, this, &FilterWidget::filterChanged);

    return lineEdit;
}

QWidget* FilterWidget::createDateRangeFilter(const FilterConfig &filter)
{
    QWidget *container = new QWidget(this);
    QHBoxLayout *layout = new QHBoxLayout(container);
    layout->setContentsMargins(0, 0, 0, 0);

    QDateEdit *startDate = new QDateEdit(QDate::currentDate().addMonths(-1), container);
    QDateEdit *endDate = new QDateEdit(QDate::currentDate(), container);

    startDate->setCalendarPopup(true);
    endDate->setCalendarPopup(true);

    layout->addWidget(startDate);
    layout->addWidget(new QLabel("至", container));
    layout->addWidget(endDate);

    container->setObjectName(filter.field + "_container");
    startDate->setObjectName(filter.field + "_start");
    endDate->setObjectName(filter.field + "_end");

    connect(startDate, &QDateEdit::dateChanged, this, &FilterWidget::onFilterValueChanged);
    connect(endDate, &QDateEdit::dateChanged, this, &FilterWidget::onFilterValueChanged);

    return container;
}

QWidget* FilterWidget::createComboBoxFilter(const FilterConfig &filter)
{
    QComboBox *comboBox = new QComboBox(this);

    // 添加选项
    for (const QString &option : filter.options) {
        comboBox->addItem(option);
    }

    connect(comboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
            this, &FilterWidget::onFilterValueChanged);

    return comboBox;
}

QMap<QString, QVariant> FilterWidget::getFilterValues() const
{
    QMap<QString, QVariant> values;

    for (const FilterConfig &filter : m_filters) {
        if (filter.type == "text") {
            QLineEdit *lineEdit = qobject_cast<QLineEdit*>(m_filterWidgets.value(filter.field));
            if (lineEdit && !lineEdit->text().isEmpty()) {
                values[filter.field] = lineEdit->text();
            }
        } else if (filter.type == "daterange") {
            QWidget *container = m_filterWidgets.value(filter.field);
            if (container) {
                QDateEdit *startDate = container->findChild<QDateEdit*>(filter.field + "_start");
                QDateEdit *endDate = container->findChild<QDateEdit*>(filter.field + "_end");

                if (startDate && endDate) {
                    QMap<QString, QVariant> dateRange;
                    dateRange["start"] = startDate->date();
                    dateRange["end"] = endDate->date();
                    values[filter.field] = dateRange;
                }
            }
        } else if (filter.type == "combobox") {
            QComboBox *comboBox = qobject_cast<QComboBox*>(m_filterWidgets.value(filter.field));
            if (comboBox && comboBox->currentIndex() > 0) {
                values[filter.field] = comboBox->currentText();
            }
        }
    }

    return values;
}

void FilterWidget::clearFilters()
{
    for (const FilterConfig &filter : m_filters) {
        if (filter.type == "text") {
            QLineEdit *lineEdit = qobject_cast<QLineEdit*>(m_filterWidgets.value(filter.field));
            if (lineEdit) {
                lineEdit->clear();
            }
        } else if (filter.type == "daterange") {
            QWidget *container = m_filterWidgets.value(filter.field);
            if (container) {
                QDateEdit *startDate = container->findChild<QDateEdit*>(filter.field + "_start");
                QDateEdit *endDate = container->findChild<QDateEdit*>(filter.field + "_end");

                if (startDate && endDate) {
                    startDate->setDate(QDate::currentDate().addMonths(-1));
                    endDate->setDate(QDate::currentDate());
                }
            }
        } else if (filter.type == "combobox") {
            QComboBox *comboBox = qobject_cast<QComboBox*>(m_filterWidgets.value(filter.field));
            if (comboBox) {
                comboBox->setCurrentIndex(0);
            }
        }
    }
}

void FilterWidget::onFilterValueChanged()
{
    // 当筛选值改变时，可以在这里添加额外的逻辑
    // 例如，可以延迟发送filterChanged信号，避免频繁触发
}

void FilterWidget::onResetFilters()
{
    clearFilters();
    emit filterChanged();
}
